class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        int h = matrix.length - 1, w = 0;
        while (h >= 0 && w < matrix[0].length) {
            if (matrix[h][w] > target) {
                h--;
            } else if (matrix[h][w] < target) {
                w++;
            } else if (matrix[h][w] == target) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        int[][] matrix = {
                {1, 4, 7, 11, 15},
                {2, 5, 8, 12, 19},
                {3, 6, 9, 16, 22},
                {10, 13, 14, 17, 24},
                {18, 21, 23, 26, 30}
        };
        int target = 100;
        boolean res = s.findNumberIn2DArray(matrix, target);
        System.out.println(res);
    }
}
